﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using DataAccessLayer;

namespace Editor
{
    public sealed class BossPresenter : IBossPresenter
    {
        private readonly IPresenterFacade Parent;

        public List<BossEnemy> Model { get; set; }

        public BossPresenter(IPresenterFacade parent)
        {
            Parent = parent;

            Model = new List<BossEnemy>();
        }

        public BossEnemy AddBoss()
        {
            int id = Parent.CreateObjectId();

            BossEnemy boss = new BossEnemy { ID = id };

            Model.Add(boss);

            return boss;
        }

        public BossEnemy GetBoss(int bossId)
        {
            return Model.Single(b => b.ID == bossId);
        }

        public void Save(int id, AircraftStats stats, RectangleProperties rects, IEnumerable<int> weaponIds, out string errMsg)
        {
            errMsg = "";
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            if (rects.CollisionRectangles.Count > 1)
                Model[index] = new SynergicBossEnemy { ID = id };

            Model[index].Hp = stats.Hp;
            Model[index].Power = stats.Power;
            Model[index].Speed = stats.Speed;
            Model[index].SourceRect = rects.SourceRectangle;

            Model[index].CollisionRectangles.AddRange(rects.CollisionRectangles);

            IEnumerable<WeaponBase> parentWeapons = from weap in Parent.GetWeapons()
                                                    from singleId in weaponIds
                                                    where weap.ID == singleId
                                                    select weap;

            IEnumerable<WeaponBase> ownedWeaps = from weap in Parent.GetWeapons()
                                                 from singleId in Model[index].WeaponIds
                                                 where weap.ID == singleId
                                                 select weap;

            int slotsLeft = Model[index].WeaponSlots - ownedWeaps.Sum(w => w.RequiredSlots);
            int weaponSlots = parentWeapons.Sum(w => w.RequiredSlots);

            if (weaponSlots > slotsLeft)
            {
                errMsg = "Add more weapon slots to boss. Weapons have more slots than boss";
                return;
            }

            Model[index].WeaponIds.AddRange(weaponIds);
        }

        public void Remove(int id)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model.RemoveAt(index);
        }

        public void AddSourceRectange(int id, Rectangle sourceRect)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].SourceRect = sourceRect;
        }

        public void AddCollisionRectangle(int id, Rectangle colRect)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].CollisionRectangles.Add(colRect);
        }

        public void SetSpriteImage(int id, string spriteImageName)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].TextureName = spriteImageName;
        }

        public void Save(int id, int hp, int power, int speed, int weaponSlots)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].Hp = hp;
            Model[index].Power = power;
            Model[index].Speed = speed;
            Model[index].WeaponSlots = weaponSlots;
        }

        public void SetSourceRectangle(int id, EditorRect sourceRect)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].SourceRect = sourceRect;
        }

        public void SetCollisionRectangle(int id, int colRIndex, EditorRect colRect)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].CollisionRectangles[colRIndex] = colRect;
        }

        public void RemoveCollisionRectangle(int id, int colRIndex)
        {
            int index = Model.IndexOf(Model.Single(b => b.ID == id));

            Model[index].CollisionRectangles.RemoveAt(colRIndex);
        }
    }
}